<?php
/**
 * Flea Site
 *
 * @copyright  Copyright (c) 2008 Movoin Studio (http://movoin.com)
 * @license    Apache License 2.0
 * @version    $Id: Singlepage.php 138 2009-05-25 06:42:38Z movoin $
 */

class Model_Singlepage
{

    var $tblContents;

    function Model_Singlepage()
    {
        $this->tblContents = & FLEA::getSingleton( 'Table_Contents' );
    }

    function getContent($colkey, $site_id, $content_id = null)
    {
        $where = array(
            array( 'col_key', $colkey ),
            array( 'site_id', (int)$site_id )
            );
        if($content_id) { $where[] = array('id', (int)$content_id ); }

        if( $content = $this->tblContents->find($where , null, 'id, title, content' ) ) {
            return $content;
        }
        return null;
    }

    function getContentList($colkey, $site_id, $fields='id,title')
    {
        $where = array(
            array( 'col_key', $colkey ),
            array( 'site_id', (int)$site_id )
        );
        return $this->tblContents->findAll($where, 'sort_id ASC, id ASC', null, $fields);
    }

    function updateContent(&$data)
    {
        $data['col_key'] = trim($data['colkey']);

        if($this->tblContents->save($data)) {
            return '保存成功!';
        }
        return '保存失败!';
    }

    function updateSort(&$data)
    {
        $data = explode(',', $data);
        foreach($data as $d) {
            $item = explode(':', $d);
            $rows[] = array('id' => $item[0], 'sort_id' => $item[1]);
        }
        if($this->tblContents->updateRowset($rows)) {
            return '保存成功!';
        }
        return '保存失败!';
    }

    function remove($removeId)
    {
        if($this->tblContents->removeByPkv((int)$removeId)) {
            return '删除成功!';
        }
        return '删除失败!';
    }

    /**
     * 返回表实例
     *
     */
    function &getTable()
    {
        return $this->tblContents;
    }
}
